package lbframework.bean.desconto;

import java.awt.Color;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.JPopupMenu;
import javax.swing.SwingConstants;
import lbframework.classe.Util;
import lbframework.classe.LBException;
import lbframework.bean.panel.LBPanel;
import lbframework.bean.table.LBColumnTable;
import lbframework.remote.ItemComboVO;

public class LBDesconto extends LBPanel {

    private JPopupMenu popUp = new JPopupMenu();
    private DescontoVO oDesconto = null;
    public Vector<DescontoVO> vDesconto = null;
    private boolean enabled = true;

    public LBDesconto() {
        initComponents();
        vDesconto = new Vector();

        cboDesconto.setEnabled(true);

    }

    public void adicionarDesconto() throws Exception {
        popUp.add(pnlPopUp);
        int x = cboDesconto.getX();
        int y = cboDesconto.getY() + cboDesconto.getHeight();

        popUp.setSize(pnlPopUp.getWidth(), pnlPopUp.getHeight());
        popUp.show(this, x, y);
        exibirVencimentos();
        txtValor.requestFocus();

    }

    public void addItem(Vector<DescontoVO> vDescontoAdd) throws Exception {
        int i = 0;

        vDesconto.addAll(vDescontoAdd);

        for (DescontoVO oAddDesconto : vDescontoAdd) {
            cboDesconto.addItem(new ItemComboVO(i, String.valueOf(oAddDesconto.valor)));
            cboDesconto.setId(-1);
            i++;
        }
    }

    public void adicionarDescontoCombo() {
        cboDesconto.removeAllItems();

        if (vDesconto != null) {
            if (vDesconto.size() != 0) {
                for (DescontoVO oDataAdd : vDesconto) {
                    cboDesconto.addItem(new ItemComboVO(oDataAdd.id, Util.formatDecimal2(oDataAdd.valor)));
                }
            }
            cboDesconto.setId(-1);
        }

    }

    private void adicionarVencimentos() throws Exception {
        if (vDesconto.size() == 1) {
            if (vDesconto.get(0).valor == 0) {
                vDesconto.clear();
            }
        }

        oDesconto = new DescontoVO();

        oDesconto.idDesconto = vDesconto.size();
        oDesconto.valor = txtValor.getDouble();

        vDesconto.add(oDesconto);

        exibirVencimentos();
        adicionarDescontoCombo();

        txtValor.setText("");
        txtValor.requestFocus();

    }

    private void exibirVencimentos() throws Exception {
        LBColumnTable[] colunas = new LBColumnTable[]{
            new LBColumnTable("Desconto(s)", 100, true, SwingConstants.LEFT, false, null),};

        Object[][] dados = new Object[vDesconto.size()][colunas.length];

        int i = 0;

        for (DescontoVO oDatas : vDesconto) {
            dados[i][0] = Util.formatDecimal2(oDatas.valor);


            i++;
        }

        tblValor.setModel(dados, colunas);
    }

    public void clear() throws Exception {
        vDesconto.clear();
        cboDesconto.removeAllItems();
    }

    public int getItemCount() throws Exception {
        return cboDesconto.getItemCount();
    }

    public int getId() throws Exception {
        return cboDesconto.getId();
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public void setEnabled(boolean value) {
        this.enabled = value;
        btnValor.setEnabled(value);
    }

    @Override
    public void setBackground(Color bg) {
        if (cboDesconto != null) {
            cboDesconto.setBackground(bg);
        }
    }

    @Override
    public synchronized void addFocusListener(FocusListener l) {
        cboDesconto.addFocusListener(l);
    }

    @Override
    public synchronized void addKeyListener(KeyListener l) {
        cboDesconto.addKeyListener(l);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPopUp = new lbframework.bean.panel.LBPanel();
        LBLabel1 = new lbframework.bean.label.LBLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblValor = new lbframework.bean.table.LBTable();
        btnEnvia = new lbframework.bean.button.LBButton();
        txtValor = new lbframework.bean.textField.LBTextField();
        btnValor = new lbframework.bean.button.LBButton();
        cboDesconto = new lbframework.bean.comboBox.LBComboBox();

        pnlPopUp.setBorder(null);

        LBLabel1.setText("% Desconto");

        tblValor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblValorKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblValor);

        btnEnvia.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/enviar.png"))); // NOI18N
        btnEnvia.setToolTipText("Enviar");
        btnEnvia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnviaActionPerformed(evt);
            }
        });
        btnEnvia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnEnviaKeyPressed(evt);
            }
        });

        txtValor.setColumns(5);
        txtValor.setMascara("Decimal 2");
        txtValor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtValorKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout pnlPopUpLayout = new javax.swing.GroupLayout(pnlPopUp);
        pnlPopUp.setLayout(pnlPopUpLayout);
        pnlPopUpLayout.setHorizontalGroup(
            pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPopUpLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlPopUpLayout.createSequentialGroup()
                        .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEnvia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(LBLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlPopUpLayout.setVerticalGroup(
            pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPopUpLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LBLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addGroup(pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEnvia, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                .addContainerGap())
        );

        setBorder(null);
        setMinimumSize(new java.awt.Dimension(89, 20));
        setPreferredSize(new java.awt.Dimension(89, 20));

        btnValor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/bean/desconto/desconto.png"))); // NOI18N
        btnValor.setToolTipText("Adicionar Desconto (F6)");
        btnValor.setFocusable(false);
        btnValor.setPreferredSize(new java.awt.Dimension(20, 20));
        btnValor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValorActionPerformed(evt);
            }
        });
        btnValor.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                btnValorFocusLost(evt);
            }
        });
        btnValor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnValorKeyPressed(evt);
            }
        });

        cboDesconto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cboDescontoKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(cboDesconto, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnValor, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cboDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnValorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValorActionPerformed
        try {
            if (enabled) {
                adicionarDesconto();
            }
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
}//GEN-LAST:event_btnValorActionPerformed

    private void btnEnviaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnviaActionPerformed
        try {
            adicionarVencimentos();
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_btnEnviaActionPerformed

    private void tblValorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblValorKeyPressed
        try {
            if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
                vDesconto.remove(tblValor.getLinhaSelecionada());
                exibirVencimentos();
            } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                this.transferFocus();
                popUp.setVisible(false);
            }
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
}//GEN-LAST:event_tblValorKeyPressed

    private void btnValorFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_btnValorFocusLost
        try {
            adicionarDescontoCombo();
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
}//GEN-LAST:event_btnValorFocusLost

    private void txtValorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtValorKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
            //this.transferFocus();
            popUp.setVisible(false);
        }
    }//GEN-LAST:event_txtValorKeyPressed

    private void cboDescontoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cboDescontoKeyPressed
        try {
            if (evt.getKeyCode() == KeyEvent.VK_F6) {
                adicionarDesconto();
            }
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_cboDescontoKeyPressed

    private void btnValorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnValorKeyPressed
        try {
            if (evt.getKeyCode() == KeyEvent.VK_F6) {
                if (enabled) {
                    adicionarDesconto();
                }
            }
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_btnValorKeyPressed

    private void btnEnviaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnEnviaKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnEnviaKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btnEnvia;
    private lbframework.bean.button.LBButton btnValor;
    private lbframework.bean.comboBox.LBComboBox cboDesconto;
    private javax.swing.JScrollPane jScrollPane1;
    private lbframework.bean.panel.LBPanel pnlPopUp;
    private lbframework.bean.table.LBTable tblValor;
    private lbframework.bean.textField.LBTextField txtValor;
    private lbframework.bean.label.LBLabel LBLabel1;
    // End of variables declaration//GEN-END:variables
}
